package com.traffic.between.service;

import java.util.regex.Matcher;
import java.util.regex.Pattern;  

/** 
 * 获取指定HTML标签的指定属性的值 
 * @param source 要匹配的源文本 
 * @param element 标签名称 
 * @param attr 标签的属性名称 
 * @return 属性值列表 
 */  
public class MatchHtmlElementAttrValueByDYKJ {  
    public static String match(String source, String element, String attr) {  
    	String result = "";
        String reg = "<" + element + "[^<>]*?\\s" + attr + "=['\"]?(.*?)['\"]?(\\s.*?)?>";  
        
        Matcher m = Pattern.compile(reg).matcher(source);  
        while (m.find()) {  
            String r = m.group(1);  
            result = r;;  
        }  
        return result;  
    }  

	public static String getElement(String content) {
		String regxpForEmbedTag = "<\\s*root\\s+([^>]*)\\s*/>";
		Matcher matcher = Pattern.compile(regxpForEmbedTag).matcher(content);
		while (matcher.find()) {
			content = "<root " + matcher.group(1) + "/>";
		}
		return content;

	}
    public static void main(String[] args) {  
        String source = "<root return=\"0\" info=\"成功\" taskid=\"20150615081233875389650\"/>";  
        String list = match(source, "root", "return");  
        System.out.println(list);  
    }  
}  